import Vue from "vue";
import VueRouter from "vue-router";
import { constantRouterMap } from "@/config/router.config";

Vue.use(VueRouter);

const router = new VueRouter({
    // mode: 'history',
    mode: "hash",
    base: process.env.BASE_URL,
    routes: [...constantRouterMap],
});

// 路由前置守卫
// 判断是否登录状态
// 以后做权限管理相关

router.beforeEach((to, from, next) => {
    if (to.matched.some((record) => record.meta.requiresAuth)) {
        if (!localStorage.getItem("token")) {
            next({
                path: "/",
                //  query: {
                //    redirect: to.fullPath
                //   }
            });
        } else {
            next();
        }
    } else {
        next();
    }
});

export default router;